package com.haoshuang.zhouzhoubang.ui.page.mine.contract.add

import android.content.Context
import android.os.Bundle
import com.eidlink.idocr.e.se
import com.haoshuang.zhouzhoubang.databinding.ActivityAddContractBinding
import com.haoshuang.zhouzhoubang.ui.dialog.ChooseDateRangeDialog
import com.haoshuang.zhouzhoubang.ui.dialog.ConfirmDialog
import com.lxj.xpopup.XPopup
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.clickWithTrigger

/**
 *  
 */
class AddContractActivity : BaseVMActivity<AddContractViewModel, ActivityAddContractBinding>() {

    companion object {
        fun launch(context: Context){
            context.startPage<AddContractActivity>()
        }
    }

    private var start_date = ""
    private var end_date = ""

    override fun initView(savedInstanceState: Bundle?) {
        binding.apply{
            titleBar.title = "上传合同"
            titleBar.leftImageView().click { finish() }

            tvTime.clickWithTrigger {
                XPopup.Builder(mActivity)
                    .asCustom(ChooseDateRangeDialog(
                        context =mActivity,
                        isEndToday = false,
                        submit = { start_time: String, end_time: String ->
                            start_date = start_time
                            end_date = end_time
                            tvTime.text = "$start_time~$end_time"
                        }))
                    .show()
            }

            tvUpload.clickWithTrigger {
                if (start_date.isEmpty() || end_date.isEmpty()){
                    toast("请选择时间")
                    return@clickWithTrigger
                }
                val images = customUploadView.getMediaList().filter { it.url?.isNotEmpty() == true }
                if (images.isEmpty()){
                    toast("请上传图片")
                    return@clickWithTrigger
                }
                if (images.size != customUploadView.getMediaList().size){
                    toast("请等待图片上传完成")
                    return@clickWithTrigger
                }
                XPopup.Builder(mActivity)
                    .asCustom(ConfirmDialog(
                        context =mActivity,
                        title = "温馨提示",
                        content = "提交的内容请确保，是否已按照所签署合同内容进行填写提交",
                        cancelStr = "返回修改",
                        confirmStr = "提交",
                        isHideCancel = false,
                        isHideAgain = false,
                        onCancel = {},
                        onConfirm = {
                            vm.addContract(start_date, end_date, images.joinToString(","){it.url.toString()})
                        }))
                    .show()

            }

        }
    }

    override fun initData() {
    }

    override fun startObserve() {
        vm.addContractData.observeState(this){
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                toast("上传成功, 等待审批")
                setResult(200)
                finish()
            }
        }
    }

}